<?php


namespace app\common\model;


use think\facade\Cache;

class CommonSearchData
{

    // 获取宿舍楼
    public static function getDormitory($model, $school_id = 0) {
        $path = str_replace('\\','/',$model);
        $key = "search".basename($path)."_dor";

        $cache_key = $school_id > 0 ? $key."_".$school_id : $key;
        $cached = Cache::store('redis')->has('ApiInfo:' . $cache_key);
        if ($cached) {
            $dormitoryList = Cache::store('redis')->get('ApiInfo:' . $cache_key);
        } else {
            $where = ['is_delete' => 0];
            if ($school_id && $school_id > 0) $where['school_id'] = $school_id;
            $dormitoryList = (new $model())->where($where)->with('dormitory')
                ->field('dormitory_id')
                ->group('dormitory_id')->select()->toArray();
            Cache::store('redis')->set('ApiInfo:' . $cache_key, $dormitoryList, 3600*24);
        }
        return $dormitoryList;
    }

    // 获取教学楼
    public static function getTeachingBuilding($model, $school_id = 0) {
        $path = str_replace('\\','/',$model);
        $key = "search".basename($path)."_building";

        $cache_key = $school_id > 0 ? $key."_".$school_id : $key;
        $cached = Cache::store('redis')->has('ApiInfo:' . $cache_key);
        if ($cached) {
            $buildingList = Cache::store('redis')->get('ApiInfo:' . $cache_key);
        } else {
            $where = [];
            if ($school_id && $school_id > 0) $where['school_id'] = $school_id;
            $buildingList = (new $model())->where($where)->with('building')
                ->field('building_id')
                ->group('building_id')->select()->toArray();
            Cache::store('redis')->set('ApiInfo:' . $cache_key, $buildingList, 3600*24);
        }
        return $buildingList;
    }
}